package com.lianniu.mgr.baseService.area.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.baseService.area.dao.IAdminAreaMapperDao;
import com.lianniu.mgr.baseService.area.model.AdminAreaVo;
import com.lianniu.mgr.baseService.area.service.IAdminAreaService;
import com.lianniu.mgr.constants.ErrorCode;
import com.lianniu.mgr.util.StringUtil;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;



/**
 * @ClassName：AdminAreaServiceImpl
 * @Description:业务实现类

 * @Since: 2016-03-23
 * @Version:1.0
 */
@Service
public class AdminAreaServiceImpl implements IAdminAreaService {
    private final static Logger logger = Logger.getLogger(AdminAreaServiceImpl.class);
    @Resource
    private IAdminAreaMapperDao adminAreaMapperDao;

    @Override
    public BaseResult getAllArea() {
        logger.info("getAllArea begin");
        List<AdminAreaVo> list = adminAreaMapperDao.getAllArea();
        logger.info("getAllArea end");
        Map<String,Object> retmap=new HashMap<String,Object>();
        retmap.put("jsonTree", list);
        return BaseResult.success(list);
    }

    @Override
    public BaseResult getParentAreaByAreaId(String areaId) {
        logger.debug("areaId:" + areaId);
        AdminAreaVo vo = adminAreaMapperDao.getParentAreaByAreaId(areaId);
        return BaseResult.success(vo);
    }

    @Override
    public BaseResult getAreaInfoByAreaId(String areaId) {
        logger.info("areaId:" + areaId);
        logger.info("getAreaInfoByAreaId begin");
        AdminAreaVo vo = adminAreaMapperDao.getAreaInfoByAreaId(areaId);
        logger.info("getAreaInfoByAreaId end");
        return BaseResult.success(vo);
    }

    //  @Transactional(value = "transactionManager", isolation = Isolation.DEFAULT,
//      propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    @Override
    public BaseResult addAreaInfo(AdminAreaVo asAreaInfoVo) {
        logger.info("addAreaInfo begin");
        logger.info("asAreaInfoVo:" +JSON.toJSONString(asAreaInfoVo));
        int result=adminAreaMapperDao.addAreaInfo(asAreaInfoVo);
        logger.info("addAreaInfo end");
        return BaseResult.success(result);
    }

    //  @Transactional(value = "transactionManager", isolation = Isolation.DEFAULT,
//      propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    @Override
    public BaseResult editAreaInfo(AdminAreaVo asAreaInfoVo) {
        logger.info("editAreaInfo begin");
        logger.info("asAreaInfoVo:" +JSON.toJSONString(asAreaInfoVo));
        int result=adminAreaMapperDao.editAreaInfo(asAreaInfoVo);
        logger.info("editAreaInfo end");
        return BaseResult.success(result);
    }

    @Override
    public BaseResult getSelectedArea(String adminAreaId) {
        logger.info("adminAreaId:" + adminAreaId);
        logger.info("getSelectedArea begin");
        List<AdminAreaVo> list =adminAreaMapperDao.getSelectedArea(adminAreaId);
        logger.info("getSelectedArea end");
        Map<String,Object> retmap=new HashMap<String,Object>();
        retmap.put("selectedTree", list);
        return BaseResult.success(list);
    }

    /**
     * @Description:根据区域level查询区域信息(adminArea)
     * @param level
     * @return BaseResult
      
     * @Date:2016年4月4日
     * @Version 1.0.0
     */
    @Override
    public BaseResult getByLevel(String level) {
        logger.info("getByLevel begin");
        logger.info("level:"+level);
        if(StringUtil.isEmpty(level)){
            return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
        }
        List<AdminAreaVo> list=adminAreaMapperDao.getByLevel(level);
        logger.info("getByLevel end");
        return BaseResult.success(list);
    }

    /**
     * @Description:根据parentId查询区域信息(adminArea)
     * @param parentId
     * @return BaseResult
      
     * @Date:2016年4月4日
     * @Version 1.0.0
     */
    @Override
    public BaseResult getByParentId(String parentId) {
        logger.info("getByParentId begin");
        logger.info("parentId:"+parentId);
        if(StringUtil.isEmpty(parentId)){
            return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
        }
        List<AdminAreaVo> list=adminAreaMapperDao.getByParentId(parentId);
        logger.info("getByParentId end");
        return BaseResult.success(list);
    }

    @Override
    public BaseResult isExistAreaCode(String id, String areaCode) {
        logger.info("isExistAreaCode begin");
        int num = adminAreaMapperDao.isExistAreaCode(id, areaCode);
        logger.info("isExistAreaCode end");
        if (num > 0) {
            return BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
        }
        return BaseResult.success(num);
    }
}
